Unload scripts created by websocket command execute_script#169368
Unload scripts created by websocket command execute_script#169368
Conversation
|
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
There was a problem hiding this comment.
Pull request overview
This PR aims to clean up Script objects created by the websocket execute_script command to avoid leaving behind resources after a script run.
Changes:
- Add a
finallyblock inhandle_execute_scriptto callscript_obj.async_unload()after execution (success or failure). - Add a new test that asserts
async_unload()is called when runningexecute_script.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
homeassistant/components/websocket_api/commands.py |
Adds cleanup via finally: script_obj.async_unload() for execute_script. |
tests/components/websocket_api/test_commands.py |
Adds a mocked-unit test asserting async_unload() is invoked. |
| finally: | ||
| script_obj.async_unload() |
There was a problem hiding this comment.
Prevent async_unload() errors from bubbling out of the finally block by stopping the script (or suppressing RuntimeError) before unloading, otherwise task cancellation or other edge cases can raise during cleanup and mask the original outcome / potentially lead to double error handling.
There was a problem hiding this comment.
This seems extremely improbable. Scripts clean up after themselves, also when they error, through the _finish method:
core/homeassistant/helpers/script.py
Lines 539 to 544 in 46297fd
The only real risk is if the cleanup here is itself cancelled, but I don't think that matters for the websocket command handler:
core/homeassistant/helpers/script.py
Lines 1872 to 1875 in 46297fd
|
Set to draft until I've taken a closer look at the bot comments. |
Proposed change
Unload scripts created by websocket command
execute_scriptType of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: